<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
//use EasyWeChat\Foundation\Application;
use EasyWeChat\Core\Exceptions\HttpException;
use EasyWeChat;
use Cache;


class WechatController extends CommonController
{
    private $menu;
    public function __construct()
    {
        parent::__construct();
        $this->menu = EasyWeChat::menu();
        view()->share([
            '_system' => 'shop',
            '_wechat' => 'am-active',
            '_shop' => 'am-in',

        ]);
    }

   public function index() //测试方法  显示微信菜单http://fangshop.app/shop/wechat/index
   {
       try {
           $buttons=Cache::rememberForever('wechat_config_menus',function(){
               $menus = $this->menu->all();
               return $menus->menu['button'];
           });

       } catch (HttpException $e) {
           $buttons = ['1'=>'菜单为空'];
       }

       return $buttons;
   }

    public function edit() //显示自定义菜单
    {
//        dd($this->menu);
        try{
            $menus = $this->menu->all();
            $buttons=$menus->menu['button'];
        }catch (HttpException $e){
            $buttons = ['1'=>'菜单为空'];
        }
//return $buttons;
        return view('admin.shop.wechats.edit')->with('buttons',$buttons);
    }

    //更新菜单
    public function update(Request $request)
    {
//        return $request->buttons;
        $buttons = wechat_menus($request->buttons);

        $this->menu->add($buttons);
//        Cache::forget('xWechat_config_menus'); //清除缓存
        return back()->with('success', '您已成功设置菜单，请取消关注后，再重新关注~');
    }

    //删除所有菜单
    public function destroy()
    {
        $this->menu->destroy();
//        Cache::forget('xWechat_config_menus');
        return back()->with('success', '您已成功删除菜单，请取消关注后，再重新关注~');
    }


}
